$(function () {
    $("#jqGrid").Grid({
        url: '../invoicing/list?status=2',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '商铺名称', name: 'shopName', index: 'good_name', width: 80},
			{label: '订单号', name: 'orderNo', index: 'order_no', width: 80},
			{label: '商铺id', name: 'shopId', index: 'shop_id', width: 80,hidden:true},
			{label: '商品名称', name: 'goodName', index: 'good_name', width: 80},
			{label: '商品id', name: 'goodId', index: 'good_id', width: 80,hidden:true},
			{label: '商品规格', name: 'goodsSpecifitionNameValue', index: 'goods_specifition_name_value', width: 80},
            {label: '销售单价(元)', name: 'goodPrice', index: 'good_price', width: 80},
			{label: '销售数量', name: 'invoicingNum', index: 'invoicing_num', width: 80},
            {label: '合计总价(元)', name: 'totalPrice', index: 'total_price', width: 80},
            {
                label: '销售时间', name: 'createTime', index: 'create_time', width: 80, formatter: function (value) {
                return transDate(value);
            }
            }
			]
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
        invoicing: {
            goodId:0,
            productId:0,
            productTypeId:0
        },
        shopss:[],
        shops:{},
        goods: [],
        products:[],
        productTypes: [],
		ruleValidate: {
		},
		q: {
		    name: '',
            goodName:''
		},
        areaBuilding:{
            name: null,
            id:0,
            type: 1}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.invoicing = {};
            vm.getProductTypes();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
        //获取所有产品类型
        getProductTypes: function () {
            Ajax.request({
                url: "../category/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.productTypes = r.list;
                }
            });
        },
		saveOrUpdate: function (event) {
            let url = vm.invoicing.id == null ? "../invoicing/save" : "../invoicing/update";
            if(vm.invoicing.invoicingNum!==null&&vm.invoicing.invoicingNum>0){
                vm.invoicing.invoicingNum = vm.invoicing.invoicingNum - 2*vm.invoicing.invoicingNum;
            }
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.invoicing),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../invoicing/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../invoicing/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.invoicing = r.invoicing;
                }
            });
		},
        //根据区域id选择店铺列表
        getShopByAreaId: function (id) {
            //加载店铺列表
            Ajax.request({
                url: "../shops/queryByAreaId/"+id,
                param:JSON.stringify(id),
                async: true,
                successCallback: function (r) {
                    vm.shopss = r.list;
                }
            });
        },
        //查询商品列表
        getGoods:function (event) {
            Ajax.request({
                url: "../goods/queryAll",
                params:JSON.stringify(vm.invoicing),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    vm.goods = r.list;
                }
            });
        },
        queryProduct:function (event) {
            if (vm.invoicing.goodId !== 0 && vm.invoicing.goodId !== '') {
                Ajax.request({
                    url: "../product/queryByGoodsId/" + vm.invoicing.goodId,
                    async: true,
                    successCallback: function (r) {
                        vm.products = r.list;
                    }
                })
            }
        },

        setProductInfo:function (event) {
            if(vm.invoicing.productId !==0 && vm.invoicing.productId !==""){
                for(let i=0;i<vm.products.length;i++){
                    let productInfo = vm.products[i];
                    if (productInfo.id === vm.invoicing.productId){
                        vm.invoicing.goodName = productInfo.goodsName;
                        vm.invoicing.goodsSpecifitionIds = productInfo.goodsSpecificationIds;
                        vm.invoicing.goodsSpecifitionNameValue = productInfo.specificationValue;
                    }
                }
            }
        },
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'goodName':vm.q.goodName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
    watch: {
        'invoicing.productTypeId': {
            handler(newId) {
                debugger;
                if(newId!==0&&newId!==''){
                    vm.getGoods();
                }
            },
            deep: true
        },
        'invoicing.goodId': {
            handler(newId) {
                debugger;
                if(newId!==0&&newId!==''){
                    vm.queryProduct();
                }
            },
            deep: true
        }
    }
});